package com.jhhc.BDMS.System.Util;

import com.google.common.base.Predicate;

import com.jhhc.BDMS.System.Pojo.BDMS_SYS_ADMINMAP_B;
import com.jhhc.BDMS.System.Serivce.BDMS_SYS_ADMINMAP_BService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class GetTxtUitl {
    @Autowired
    private BDMS_SYS_ADMINMAP_BService bdms_sys_adminmap_bService;

    public  int in(BDMS_SYS_ADMINMAP_B b ){
      return   bdms_sys_adminmap_bService.insert(b);
    }

    public  int in2(List<BDMS_SYS_ADMINMAP_B> b ){

        for (int i=0;i<b.size();i++){

            bdms_sys_adminmap_bService.insert(b.get(i));
            System.out.println("di" +i+ "ci");
         // return i ;
        }
        return 0;
    }

    //读取io
    public static String readPointLIne(String fileName,int readLine) {
        String line;//读取每行的内容
        try (
                BufferedReader br = Files.newBufferedReader(Paths.get(fileName))){
            int i=0;
            //每次读取一行，一行一行的读取 br.readLine()
            while ((line = br.readLine()) != null ) {

                i++;
                if(i==readLine){
                   // if (line.substring())
                    return line;
                }
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return null;
    }



    /**
     * 开始读取的行数
     */
    public static String getString(int index,String path) throws Exception{
        BufferedReader br = new BufferedReader(new FileReader(new File(path)));
        StringBuffer sb = new StringBuffer();
        String temp = null;
        int count = 0;
        while((temp = br.readLine() ) != null){
            count ++;
            if(count >= index){
                if (temp.charAt(0) == '1'){
                    sb.append(temp+";");
                }
            }
        }
        return sb.toString();
    }


    //遍历文件夹下所有的文件
    //获取目录下所有
    public   List<String> getMIFMID(String path) throws Exception {
        File file = new File(path);
        String[] files = file.list();
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            File file1 = new File(path, files[i]);
            strings.add(path + "\\" + file1.getName());// 添加文件全路径名
        }


        Predicate<String> contain = n -> n.contains("t");


        List<String> stringList = strings.stream().filter((java.util.function.Predicate<? super String>) contain).collect(Collectors.toList());
        return stringList;
    }


    //获取到列表集合文件后
    public   BDMS_SYS_ADMINMAP_B InsertPoint(){
        BDMS_SYS_ADMINMAP_B bdms_sys_adminmap_b  = new BDMS_SYS_ADMINMAP_B();
        String path = "E:\\work\\RiverStrong\\相关项目\\宁波项目\\区划txt";
        try {
            List<String> a =   getMIFMID(path);
            for (String c:a){
                if (Objects.equals(c.substring(38, 40), "市名")){
                    bdms_sys_adminmap_b.setArealevel(1);
                    //MID文件 设置point的 name
                    if (c.substring(41,44).equals("MID")){


                        bdms_sys_adminmap_b.setPid(2206);
                        String pointname  = readPointLIne(c,1);
                        bdms_sys_adminmap_b.setPointname(pointname);
                    } else { //MIF文件设置 Pointxy  和边界
                        String  XY  = readPointLIne(c,11);
                        System.out.println(XY);
                        String ponitXY= XY.substring(7,25);
                        bdms_sys_adminmap_b.setPointxy(ponitXY);

                    }

                }else {
                    if (c.startsWith("MIF", 41)){
                        String boundarys   = getString(11,path);
                        bdms_sys_adminmap_b.setBoundarys(boundarys);
                    }else {
                        String areaname  = readPointLIne(c,1);
                        bdms_sys_adminmap_b.setAreaname(areaname);
                    }

                }

            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return bdms_sys_adminmap_b;
    }


    //获取到列表集合文件后
    public    List<BDMS_SYS_ADMINMAP_B> InsertPoint2(){
        int res=0;
        List<BDMS_SYS_ADMINMAP_B> bdms_sys_adminmap_bs = new ArrayList<>();
        String path = "E:\\work\\RiverStrong\\相关项目\\珠海项目\\珠海潮位市界";
        try {
            List<String> a =   getMIFMID(path);
            //获取到文件
            for (String c:a) {
                //判断MID和MIF
                BDMS_SYS_ADMINMAP_B bdms_sys_adminmap_b  = new BDMS_SYS_ADMINMAP_B();
                //设置项目id
                bdms_sys_adminmap_b.setPid(2206);
                //设置名称
                String rname =  readPointLIne(c,1);
//                if (rname.charAt(0) == '"') {
//                    bdms_sys_adminmap_b.setAreaname(rname);
//                    System.out.println(rname);
//                }
                bdms_sys_adminmap_b.setArealevel(2);
                String pname =  readPointLIne(c,2);
//                if (rname.charAt(0) == '"') {
//                    bdms_sys_adminmap_b.setPointname(pname);
//                    System.out.println(pname);
//                }
                String point =  readPointLIne(c,3);
                if (Objects.equals(point.substring(0, 6), "Point ")) {
                    bdms_sys_adminmap_b.setPointxy(point.substring(6));
                    System.out.println(point.substring(6));
                }
                String boundarys = getString(6,c);
                List<String> bs = Arrays.asList(boundarys.split(";"));
                System.out.println(bs.size());
                bdms_sys_adminmap_b.setBoundarys(boundarys);
                System.out.println(boundarys);

                bdms_sys_adminmap_bs.add(bdms_sys_adminmap_b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bdms_sys_adminmap_bs;
    }


    public static void main(String[] args) {
        GetTxtUitl g = new GetTxtUitl();
     //   g.InsertPoint2();
        try {
           String res = getString(0,"E:\\work\\RiverStrong\\相关项目\\珠海项目\\行政区划\\珠海市界.txt");


          int a= res.split(";").length;
            System.out.println(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




//    public static void main(String[] args) {
//        String path = "E:\\work\\RiverStrong\\相关项目\\宁波项目\\区划txt";
//        try {
//            List<String> a =   getMIFMID(path);
//            for (String c:a){
//                System.out.println(c.substring(38,40));
//                System.out.println(c.substring(41,44));
//            }
//
//            System.out.println(InsertPoint().toString());
//
//            String bxy = InsertPoint().getBoundarys();
//            List<String>  b =  Arrays.asList(bxy.split(";").clone());
//            System.out.println(b.size());
//            GetTxtUitl h = new GetTxtUitl();
//            h.in(InsertPoint());
//
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//
//    }
}
